package cn.iocoder.yudao.module.system.api.dept;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptSaveReqVO;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptSimpleDTO;
import cn.iocoder.yudao.module.system.enums.ApiConstants;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.PermitAll;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@FeignClient(name = ApiConstants.NAME)
@Tag(name = "RPC 服务 - 部门")
public interface DeptApi {

    String PREFIX = ApiConstants.PREFIX + "/dept";

    @GetMapping(PREFIX + "/get")
    @Operation(summary = "获得部门信息")
    @Parameter(name = "id", description = "部门编号", example = "1024", required = true)
    CommonResult<DeptRespDTO> getDept(@RequestParam("id") Long id);

    @GetMapping(PREFIX + "/get/NotCheckDataPermission")
    @Operation(summary = "获得部门信息-不校验数据权限接口")
    @Parameter(name = "id", description = "部门编号", example = "1024", required = true)
    CommonResult<DeptRespDTO> getDeptNotCheckDataPermission(@RequestParam("id") Long id);

    @GetMapping(PREFIX + "/list")
    @Operation(summary = "获得部门信息数组")
    @Parameter(name = "ids", description = "部门编号数组", example = "1,2", required = true)
    CommonResult<List<DeptRespDTO>> getDeptList(@RequestParam("ids") Collection<Long> ids);

    @GetMapping(PREFIX + "/list/NotCheckDataPermission")
    @Operation(summary = "获得部门信息数组-不校验数据权限接口")
    @Parameter(name = "ids", description = "部门编号数组", example = "1,2", required = true)
    CommonResult<List<DeptRespDTO>> getDeptListNotCheckDataPermission(@RequestParam("ids") Collection<Long> ids);

    @GetMapping(PREFIX + "/valid")
    @Operation(summary = "校验部门是否合法")
    @Parameter(name = "ids", description = "部门编号数组", example = "1,2", required = true)
    CommonResult<Boolean> validateDeptList(@RequestParam("ids") Collection<Long> ids);

    @PostMapping(PREFIX + "/create")
    @Operation(summary = "创建部门")
    CommonResult<Long> createDept(@Valid @RequestBody DeptSaveReqVO createReqVO);

    @PutMapping(PREFIX + "/update")
    @Operation(summary = "更新部门")
    CommonResult<Boolean> updateDept(@Valid @RequestBody DeptSaveReqVO updateReqVO);

    @DeleteMapping(PREFIX + "/delete")
    @Operation(summary = "删除部门")
    CommonResult<Boolean> deleteDept(@RequestParam("id") Long id);

    /**
     * 获得指定编号的部门 Map
     *
     * @param ids 部门编号数组
     * @return 部门 Map
     */
    default Map<Long, DeptRespDTO> getDeptMap(Collection<Long> ids) {
        List<DeptRespDTO> list = getDeptList(ids).getCheckedData();
        return CollectionUtils.convertMap(list, DeptRespDTO::getId);
    }

    /**
     * 获得指定编号的部门 Map -不校验数据权限的部门转换接口
     *
     * @param ids 部门编号数组
     * @return 部门 Map
     */
    default Map<Long, DeptRespDTO> getDeptMapNotCheckDataPermission(Collection<Long> ids) {
        List<DeptRespDTO> list = getDeptListNotCheckDataPermission(ids).getCheckedData();
        return CollectionUtils.convertMap(list, DeptRespDTO::getId);
    }




    @GetMapping(PREFIX + "/list-child")
    @Operation(summary = "获得指定部门的所有子部门")
    @Parameter(name = "id", description = "部门编号", example = "1024", required = true)
    CommonResult<List<DeptRespDTO>> getChildDeptList(@RequestParam("id") Long id);

    @GetMapping(PREFIX + "/all-dept")
    @Operation(summary = "获取所有部门精简信息列表", description = "只包含被开启的部门，主要用于前端的下拉选项")
    CommonResult<List<DeptSimpleDTO>> getAllSimpleDeptListNotCheckDataPermission();

}
